package cn.learn.validate;

import lombok.Getter;

import java.time.LocalDateTime;

/**
 * 验证码
 *
 * @author huangyezhan
 * @date 2020年02月18日15:02
 */
@Getter
public class ValidateCode {
    /**
     * 验证码
     */
    private String code;

    /**
     * 过期时间（秒）
     */
    private LocalDateTime expireTime;

    /**
     * 验证总次数，超过了规定次数，则锁定用户（可配置）
     */
    private int totalValidations = 6;

    /**
     * 验证的次序，每验证失败一次 +1，大于 totalValidations 锁定账户
     */
    private int validateNum = 0;

    /**
     * 刷新验证码次数（不管验不验证，没发一次记录一次）
     */
    private int refreshNum = 1;

    /**
     * 是否过期
     *
     * @return 返回判断结果
     */
    public boolean isExpried() {
        return LocalDateTime.now().isAfter(this.expireTime);
    }


    //==========================================================================================

    public ValidateCode() {
    }

    public ValidateCode(String code, LocalDateTime expireTime, int totalValidations) {
        this.code = code;
        this.expireTime = expireTime;
        this.totalValidations = totalValidations;
    }

    public ValidateCode(String code, int expireTime, int totalValidations) {
        this.code = code;
        this.expireTime = LocalDateTime.now().plusSeconds(expireTime);
        this.totalValidations = totalValidations;
    }


    public void setCode(String code) {
        this.code = code;
    }

    /**
     * 设置过期时间（秒）
     *
     * @param expireTime 过期时长
     */
    public void setExpireTime(LocalDateTime expireTime) {
        this.expireTime = expireTime;
    }

    public void setExpireTime(int expireTime) {
        this.expireTime = LocalDateTime.now().plusSeconds(expireTime);
    }
    public void setExpireTime(String expireTime) {
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        this.expireTime  = LocalDateTime.parse(expireTime);
    }

    public void setValidateNum(int validateNum) {
        this.validateNum = validateNum;
    }

    public void setTotalValidations(int totalValidations) {
        this.totalValidations = totalValidations;
    }

    public void setRefreshNum(int refreshNum) {
        this.refreshNum = refreshNum;
    }

}
